<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="商户Id" prop="trainingSchoolId">
        <el-input
          v-model="queryParams.trainingSchoolId"
          placeholder="请输入商户Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度id" prop="topicId">
        <el-input
          v-model="queryParams.topicId"
          placeholder="请输入年度id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书类别:bk_type" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入图书类别:bk_type"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推荐书" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入推荐书"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入书名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预售价" prop="expectPrice">
        <el-input
          v-model="queryParams.expectPrice"
          placeholder="请输入预售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原价" prop="originalPrice">
        <el-input
          v-model="queryParams.originalPrice"
          placeholder="请输入原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示原价" prop="showOriginalPrice">
        <el-input
          v-model="queryParams.showOriginalPrice"
          placeholder="请输入是否显示原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示出本社" prop="showPublisher">
        <el-input
          v-model="queryParams.showPublisher"
          placeholder="请输入是否显示出本社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示作者" prop="showAuthor">
        <el-input
          v-model="queryParams.showAuthor"
          placeholder="请输入是否显示作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否必选" prop="required">
        <el-input
          v-model="queryParams.required"
          placeholder="请输入是否必选"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购物车中商品是否选择状态" prop="checked">
        <el-input
          v-model="queryParams.checked"
          placeholder="请输入购物车中商品是否选择状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="addTime">
        <el-date-picker clearable
                        v-model="queryParams.addTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="逻辑删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="${comment}" align="center" prop="id"/>
      <el-table-column label="商户Id" align="center" prop="trainingSchoolId"/>
      <el-table-column label="年度id" align="center" prop="topicId"/>
      <el-table-column label="学校类别:低幼,小学,初中,高中" align="center" prop="schoolType"/>
      <el-table-column label="图书类别:bk_type" align="center" prop="typeId"/>
      <el-table-column label="类型名称" align="center" prop="typeName"/>
      <el-table-column label="推荐书" align="center" prop="goodsId"/>
      <el-table-column label="书名" align="center" prop="goodsName"/>
      <el-table-column label="出版社" align="center" prop="publisher"/>
      <el-table-column label="作者" align="center" prop="author"/>
      <el-table-column label="预售价" align="center" prop="expectPrice"/>
      <el-table-column label="原价" align="center" prop="originalPrice"/>
      <el-table-column label="是否显示原价" align="center" prop="showOriginalPrice"/>
      <el-table-column label="是否显示出本社" align="center" prop="showPublisher"/>
      <el-table-column label="是否显示作者" align="center" prop="showAuthor"/>
      <el-table-column label="售价" align="center" prop="price"/>
      <el-table-column label="是否必选" align="center" prop="required"/>
      <el-table-column label="购物车中商品是否选择状态" align="center" prop="checked"/>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="逻辑删除" align="center" prop="deleted"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改年度书单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户Id" prop="trainingSchoolId">
          <el-input v-model="form.trainingSchoolId" placeholder="请输入商户Id"/>
        </el-form-item>
        <el-form-item label="年度id" prop="topicId">
          <el-input v-model="form.topicId" placeholder="请输入年度id"/>
        </el-form-item>
        <el-form-item label="图书类别:bk_type" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入图书类别:bk_type"/>
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称"/>
        </el-form-item>
        <el-form-item label="推荐书" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入推荐书"/>
        </el-form-item>
        <el-form-item label="书名" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入书名"/>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者"/>
        </el-form-item>
        <el-form-item label="预售价" prop="expectPrice">
          <el-input v-model="form.expectPrice" placeholder="请输入预售价"/>
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价"/>
        </el-form-item>
        <el-form-item label="是否显示原价" prop="showOriginalPrice">
          <el-input v-model="form.showOriginalPrice" placeholder="请输入是否显示原价"/>
        </el-form-item>
        <el-form-item label="是否显示出本社" prop="showPublisher">
          <el-input v-model="form.showPublisher" placeholder="请输入是否显示出本社"/>
        </el-form-item>
        <el-form-item label="是否显示作者" prop="showAuthor">
          <el-input v-model="form.showAuthor" placeholder="请输入是否显示作者"/>
        </el-form-item>
        <el-form-item label="售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入售价"/>
        </el-form-item>
        <el-form-item label="是否必选" prop="required">
          <el-input v-model="form.required" placeholder="请输入是否必选"/>
        </el-form-item>
        <el-form-item label="购物车中商品是否选择状态" prop="checked">
          <el-input v-model="form.checked" placeholder="请输入购物车中商品是否选择状态"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
                          v-model="form.addTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listGoods, getGoods} from "@/api/jiaofu/topic_goods";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年度书单表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainingSchoolId: null,
        topicId: null,
        schoolType: null,
        typeId: null,
        typeName: null,
        goodsId: null,
        goodsName: null,
        publisher: null,
        author: null,
        expectPrice: null,
        originalPrice: null,
        showOriginalPrice: null,
        showPublisher: null,
        showAuthor: null,
        price: null,
        required: null,
        checked: null,
        addTime: null,
        deleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        topicId: [
          {required: true, message: "年度id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年度书单列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingSchoolId: null,
        topicId: null,
        schoolType: null,
        typeId: null,
        typeName: null,
        goodsId: null,
        goodsName: null,
        publisher: null,
        author: null,
        expectPrice: null,
        originalPrice: null,
        showOriginalPrice: null,
        showPublisher: null,
        showAuthor: null,
        price: null,
        required: null,
        checked: null,
        addTime: null,
        updateTime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加年度书单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改年度书单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除年度书单编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
